package project.dade.cn.bst.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import butterknife.InjectView;
import project.dade.cn.bst.MainActivity;
import project.dade.cn.bst.R;
import project.dade.cn.bst.adapter.base.TimeAdapter;
import project.dade.cn.bst.base.ZJPBaseBarActivity;
import project.dade.cn.bst.manager.ApiConfig;
import project.dade.cn.bst.model.GsonUtils;
import project.dade.cn.bst.model.ResultTimeBean;
import project.dade.cn.bst.model.TimeRequestsModel;
import project.dade.cn.bst.network.callback.ResultCallback;
import project.dade.cn.bst.network.request.OkHttpRequest;
import project.dade.cn.bst.utils.AppUtil;
import project.dade.cn.bst.utils.NetRequestUtil;
import project.dade.cn.bst.utils.ToastUtil;
import project.dade.cn.bst.views.EmptyLayout;

/**
 * Created by vain zhou on 2016/6/17.
 */
public class ChooseCarTimeActivity extends ZJPBaseBarActivity {
    @InjectView(R.id.error_layout)
    EmptyLayout mErrorLayout;
    @InjectView(R.id.tv_time)
    RadioButton time;
    @InjectView(R.id.tv_time1)
    RadioButton time1;
    @InjectView(R.id.tv_time2)
    RadioButton time2;
    @InjectView(R.id.gridView)
    GridView gridView;
    @InjectView(R.id.radio_group1)
    RadioGroup rGroup;
    private String index="0";
    private List<String> times;
    ChooseCarTimeActivity activity = this;
    //时间段数据model
    private ResultTimeBean mResultTimeBean;

    private String day;
    private String day2;
    private String day3;
    private String type;
    private String money;
    private  String number;
    private String name="";
    private String phone="";
    private String address="";
    private String carnumber="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choosetime);
        initPageViewListener();
        initProcess();
        int id = getIntent().getIntExtra("id", 0);
        name=getIntent().getStringExtra("name");
        phone=getIntent().getStringExtra("moblie");
        address=getIntent().getStringExtra("address");
        carnumber=getIntent().getStringExtra("carnumber");
        type=getIntent().getStringExtra("type");
        money=getIntent().getStringExtra("money");
        number=getIntent().getStringExtra("number");

        switch (id) {
            case 1:
                rGroup.check(time.getId());
                break;
            case 2:
                rGroup.check(time1.getId());
                break;
            case 4:
                rGroup.check(time2.getId());
                break;
            default:
                break;
        }
        requestGetTime();

    }

    private void initProcess() {
        int id = getIntent().getIntExtra("name", 0);
        switch (id) {
            case 1:
                time.setChecked(true);
                time1.setChecked(false);
                time2.setChecked(false);
                break;

            case 2:
                time.setChecked(false);
                time1.setChecked(true);
                time2.setChecked(false);
                break;
            case 3:
                time.setChecked(false);
                time1.setChecked(false);
                time2.setChecked(true);
            default:
                break;
        }
    }
    // 时间段
    private void requestGetTime() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("type", "day");
        new OkHttpRequest.Builder()
                .url(ApiConfig.getHostAddress() + ApiConfig.SERVER_URL.ToBuyTime).params(params)
                .post(new ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {

                        ToastUtil.showShort(ChooseCarTimeActivity.this, "网络相应失败请稍后再试");
                    }
                    @Override
                    public void onResponse(String response) {
                        if (TextUtils.isEmpty(NetRequestUtil.requestsError(response, "获取天数"))) {
                            TimeRequestsModel model= new Gson().fromJson(response, TimeRequestsModel.class);
                            if (model != null && model.getResult() == 0) {
                                //获取请求成功
                                TreeMap<String, ArrayList<String>> m=model.getData();
                                Set<String> ss=m.keySet();
                                for(String s:ss){
                                    ArrayList<String> i=  m.get(s);
                                    day=i.get(0);
                                    day2=i.get(1);
                                    day3=i.get(2);
                                    time.setText(i.get(0));
                                    time1.setText(i.get(1));
                                    time2.setText(i.get(2));
                                }
                            }

                        } else {
                            ToastUtil.showShort(ChooseCarTimeActivity.this, NetRequestUtil.requestsError(response, ""));

                        }
                    }

                });

    }
    @Override
    protected void onResume() {
        super.onResume();
        initRequest();

    }
    private void initRequest() {
        if (AppUtil.isNetworkAvailable(this)) {
            requestGettimesole();
        } else {
            mErrorLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
        }

    }

    private void requestGettimesole() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("type", "time");
        params.put("index",index);
        new OkHttpRequest.Builder()
                .url(ApiConfig.getHostAddress() + ApiConfig.SERVER_URL.ToBuyTime).params(params)
                .post(new ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {
                        AppUtil.dismissLoadingDialog();
                        ToastUtil.showShort(ChooseCarTimeActivity.this, "网络相应失败请稍后再试");
                    }

                    @Override
                    public void onResponse(String response) {
                        if (TextUtils.isEmpty(NetRequestUtil.requestsError(response, "获取时间点"))) {
                            mResultTimeBean = GsonUtils.jsonToBean(response, ResultTimeBean.class);
                            if(mResultTimeBean.getResult() == 0){
                                AppUtil.dismissLoadingDialog();
                                times = mResultTimeBean.getData().getTimes();
                                TimeAdapter mTimeAdapter = new TimeAdapter(ChooseCarTimeActivity.this, times);
                                gridView.setAdapter(mTimeAdapter);
                            }


                        } else {
                            AppUtil.dismissLoadingDialog();
                            ToastUtil.showShort(ChooseCarTimeActivity.this, NetRequestUtil.requestsError(response, ""));

                        }
                    }

                });

    }
    protected void initPageViewListener() {
        rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton t = (RadioButton) findViewById(checkedId);
                if (t == time) {
                    index="0";
                    AppUtil.showLoadingDialog(activity, "加载中");
                    requestGettimesole();
                } else if (t == time1) {
                    index="1";
                    day=day2;
                    AppUtil.showLoadingDialog(activity, "加载中");
                    requestGettimesole();
                } else if (t == time2) {
                    index="2";
                    day=day3;
                    AppUtil.showLoadingDialog(activity, "加载中");
                    requestGettimesole();
                }
            }
        });

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent mIntentRegInfo = new Intent(ChooseCarTimeActivity.this,WashCarSubmitActivity.class);
                mIntentRegInfo.putExtra("time", day+times.get(position));
                mIntentRegInfo.putExtra("type",type);
                mIntentRegInfo.putExtra("money",money);
                mIntentRegInfo.putExtra("unmber",number);
                mIntentRegInfo.putExtra("name",name);
                mIntentRegInfo.putExtra("eare",address);
               mIntentRegInfo.putExtra("moblie",phone);
                mIntentRegInfo.putExtra("carnumber",carnumber);
                startActivity(mIntentRegInfo);
                finish();
            }
        });
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

